package org.wheel.plugins.starter.jdbc.pool.config;


import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.wheel.plugins.starter.autoconfigure.DynamicDataSourceAutoConfiguration;
import org.wheel.plugins.starter.autoconfigure.DynamicDataSourceProperties;
import org.wheel.plugins.starter.autoconfigure.OfficialDataSourceReuseConfiguration;

/**
 * 自动装配类
 */
@AutoConfigureAfter({DynamicDataSourceAutoConfiguration.class, OfficialDataSourceReuseConfiguration.class})
@EnableConfigurationProperties({DaoConfigProperties.class})
//@Import({DaoRegistrar.class, ResourceLoader.class})
@Import({YamlConfigLoader.class, DaoRegistrar.class})
public class DaoAutoConfiguration {

    // 直接使用 LogFactory 创建日志对象，Hutool 会自动检测项目中的日志框架（优先级：Slf4j > Log4j > Logback > JDK Logging）
    private static final Log log = LogFactory.get();

//    @Bean
//    @ConditionalOnMissingBean
//    public DynamicDataSourceProperties dynamicDataSourceProperties() {
//        return new DynamicDataSourceProperties();
//    }

//
//    // 供别的 Bean 拿
//    private final DaoConfigProperties properties;
//
//    // 供别的 Bean 拿
//    @Bean
//    public DaoConfigProperties daoConfigProperties() {
//        return properties;
//    }
//
//    @Bean
//    @ConditionalOnMissingBean
//    public ResourceLoader resourceLoader() {
//        return new DefaultResourceLoader(); // 使用默认实现
//    }

//    public DaoAutoConfiguration(DaoConfigProperties properties ,
//                                ResourceLoader resourceLoader) throws IOException, XPathExpressionException,
//            ParserConfigurationException, SAXException {
//        System.out.println("====================   DaoAutoConfiguration  ========================");
//        this.properties = properties;
//        // 1. 解析 XML pattern = classpath*:mapper/*.xml
//        for (String pattern : properties.getXmlPath()){
//            Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader)
//                    .getResources(pattern);
//            XmlMapperLoader.load(resources);
//        }
//    }


//    public DaoAutoConfiguration(DaoConfigProperties properties) throws IOException, XPathExpressionException,
//            ParserConfigurationException, SAXException {
//        System.out.println("====================   DaoAutoConfiguration  ========================");
//        this.properties = properties;
//        // 1. 解析 XML pattern = classpath*:mapper/*.xml
//
//        ResourceLoader resourceLoader = new DefaultResourceLoader();
//        ResourcePatternResolver resolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
//        for (String pattern : properties.getXmlPath()) {
//             XmlMapperLoader.load(resolver.getResources(pattern));
//        }
//    }

    public DaoAutoConfiguration(){
        log.trace("====================   DaoAutoConfiguration 1  ========================");
    }

//    public DaoAutoConfiguration(DaoConfigProperties properties){
//        log.info("====================   DaoAutoConfiguration 2 ========================");
//        this.properties = properties;
//        ResourceLoader resourceLoader = resourceLoader();
//        // 1. 解析 XML pattern = classpath*:mapper/*.xml
//        for (String pattern : properties.getXmlPath()){
//            try{
//                Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader)
//                        .getResources(pattern);
//
////                Resource[] resources = applicationContext.getResources(pattern);
//                XmlMapperLoader.load(resources);
//            } catch (Exception e) {
//                log.error("无法加载 XML 配置文件：" + pattern, e);
//            }
//        }
//    }


}
